#!/usr/bin/python
# -*- coding: utf-8 -*-#

# string to XML cleaner

import os
import sys
import plistlib
import codecs

scriptName = sys.argv[0].split('/').pop()
usage = '\nUsage:\nEnter the string to convert as the first argument\n' + scriptName + ' mystring\nOut comes the correct XML string in brackets\n'
myText = ""
def convertXML(theString):
	myplist = dict(myout = theString)
	plistlib.writePlist(myplist, '/tmp/c38402a0-9467-4936-8311-7958f34c25db.plist')
	return

def cleanUp():
	global myText
	if os.path.exists('/tmp/c38402a0-9467-4936-8311-7958f34c25db.plist'):
		tempfile = open('/tmp/c38402a0-9467-4936-8311-7958f34c25db.plist', 'r')
		myText = tempfile.readlines()
		tempfile.close()
		os.remove('/tmp/c38402a0-9467-4936-8311-7958f34c25db.plist')
		return myText
	else:
		print('Tempfile not found, please try again')
		raise(SystemExit(1))


if len(sys.argv) == 1:
	print(usage)
	raise(SystemExit(1))
elif len(sys.argv) == 2:
	convertXML((sys.argv[1]).decode("utf-8"))
	cleanUp()
	print(myText[5]).strip().replace('<string>', '').replace('</string>', '')
	raise(SystemExit(0))
else:
	print(usage)
	raise(SystemExit(1))




